<template>
    <el-dialog title="拒绝通过" :visible.sync="dialogFormVisible" width="400px">
        <el-timeline :reverse="true">
            <el-timeline-item
                v-for="(activity, index) in formData.denialReason"
                :key="index"
                :timestamp="activity.timestamp"
            >{{activity.content}}</el-timeline-item>
        </el-timeline>
        <el-form ref="formData" :rules="rules" :model="formData">
            <el-form-item prop="customizeDenialReason">
                <el-input
                    type="textarea"
                    :autosize="{ minRows: 3, maxRows: 5}"
                    placeholder="请输入内容"
                    v-model="formData.customizeDenialReason"
                ></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button
                type="primary"
                @click="submitForm('formData')"
                size="small"
                :disabled="loading"
                :icon="loading?'el-icon-loading':'el-icon-circle-plus-outline'"
            >确定</el-button>
        </div>
    </el-dialog>
</template>
<script>
const ERR_OK = 200;
import { formatDate } from "assets/js/date";
export default {
    props: {},
    data() {
        return {
            activities: [
                {
                    content: "活动按期开始",
                    timestamp: "2018-04-15"
                },
                {
                    content: "通过审核",
                    timestamp: "2018-04-13"
                },
                {
                    content: "创建成功",
                    timestamp: "2018-04-11"
                }
            ],
            formData: {
                checkStatus: "",
                checkerUserId: this.$store.getters.userId,
                denialReason: [],
                visionDemandId: "",
                visionDemandStatusId: "",
                customizeDenialReason: ""
            },
            rules: {
                customizeDenialReason: [
                    {
                        required: true,
                        message: "请输入不通过原因",
                        trigger: "blur"
                    }
                ]
            },
            dialogFormVisible: false,
            loading: false
        };
    },
    methods: {
        show(item, child, status) {
            this.formData.checkStatus = status;
            this.formData.visionDemandId = item.visionDemandId;
            this.formData.visionDemandStatusId = item.visionDemandStatusId;
            if (child.denialReason) {
                this.formData.denialReason = JSON.parse(child.denialReason);
            }
            this.formData.customizeDenialReason = "";
            if (status === 1) {
                this.checkVisionDemand();
            } else {
                this.dialogFormVisible = true;
            }
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.checkVisionDemand();
                } else {
                    return false;
                }
            });
        },
        checkVisionDemand() {
            this.$confirm("是否确定提交", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    let formData = JSON.parse(JSON.stringify(this.formData));
                    if (formData.customizeDenialReason.trim()) {
                        formData.denialReason.push({
                            content: formData.customizeDenialReason.trim(),
                            timestamp: formatDate(
                                new Date(),
                                "yyyy-MM-dd hh:mm:ss"
                            )
                        });
                    }
                    formData.denialReason = JSON.stringify(
                        formData.denialReason
                    );
                    this.loading = true;
                    this.$ajax({
                        method: "post",
                        url:
                            this.GLOBAL.oaVisionDemand +
                            "/visionDemandStatus/checkVisionDemand",
                        data: formData
                    }).then(res => {
                        this.loading = false;
                        if (res.data.code === ERR_OK) {
                            this.$message.success(res.data.msg);
                            this.dialogFormVisible = false;
                            this.$emit("loadData");
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    });
                })
                .catch(() => {});
        }
    }
};
</script>
<style lang="stylus" scoped>
.el-form-item {
    margin-bottom: 10px;

    .el-date-editor {
        width: 100%;
    }
}

.el-timeline {
    overflow: auto;
    max-height: 250px;
}
</style>
